package concept.misc;

import java.text.DateFormat;
import java.util.Date;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedOperationParameter;
import org.springframework.jmx.export.annotation.ManagedOperationParameters;
import org.springframework.jmx.export.annotation.ManagedResource;

@ManagedResource(objectName="scjp:name=caller")
public class Caller implements Runnable
{
	public static void main(String[] args)
	{
		Thread.currentThread().setName("Main Thread");
		System.out.println(Thread.currentThread().getId());
		Runnable c = (Caller)new ClassPathXmlApplicationContext("classpath:jmx-config.xml").getBean("caller");
		Thread t = new Thread(c);
		t.setName("Labor thread");
		t.start();

	}
	@Override
	public void run()
	{
		
		while(true)
		{
			try
			{
				Thread.sleep(1000);
				printCurrentTime(new Date());
			}
			catch (InterruptedException e)
			{
			}
		}
	}

	@ManagedOperation(description="Method printing the current time")
	@ManagedOperationParameters
	({
		@ManagedOperationParameter(name="date", description="Date object")
	})
	public void printCurrentTime(Date date)
	{
		DateFormat df = DateFormat.getTimeInstance(DateFormat.MEDIUM);
		
		System.out.println(Thread.currentThread().getId()+"> "+df.format(date));
	}
}
